package umls.interfaces.Pathways;

import java.util.LinkedList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParser extends DefaultHandler {
	private List<String> geneList;
	// private String last=null;
	boolean gene = false;
	boolean db = false;
	boolean read = false;

	public SaxParser() {
		geneList = new LinkedList<String>();
		// System.out.println("Handler SAX creato");
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		// System.out.println(qName);
		if (qName.equalsIgnoreCase("db")) {
			// System.out.println(localName+" "+ qName);
			db = true;

		} else {
			if (qName.equalsIgnoreCase("id") && gene) {
				read = true;
			}

		}

	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (db && !gene) {
			String strValue = new String(ch, start, length);
			if (strValue.equalsIgnoreCase("Gene Symbol")) {
				gene = true;
				// System.out.println("Trovato simbolo Gene");

			}
			db = false;
		} else {

			if (read) {
				String strValue2 = new String(ch, start, length);
				geneList.add(strValue2);
				gene = false;
				db = false;
				read = false;
			}
		}

	}

	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {

	}

	public List<String> getGenes() {
		return geneList;
	}
}
